package com.simpleframe.web.action;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.xwork2.Preparable;

@Results( {   
    @Result(name = "main", location = "main.jsp"),
    @Result(name = "grid", location = "grid.jsp"),
    @Result(name = "edit", location = "edit.jsp"),
    @Result(name = "success", location = "main.shtml", type="redirect")
})
public abstract class CrudActionSupport extends SimpleframeAction {
  
    // -- CRUD Action函数 --//  
	private Long id;
	
	/**
	 * 显示一个操作的主界面
	 */
	public abstract String main() throws Exception;
	
    /** 
     * Action函数,显示Entity列表界面. 建议return SUCCESS. 
     */  
    public abstract String grid() throws Exception;
    
    public abstract String create() throws Exception;
  
    /** 
     * Action函数,显示新增或修改Entity界面. 建议return EDIT. 
     */  
    //@Override  
    public abstract String edit() throws Exception;  
  
    /** 
     * Action函数,新增或修改Entity. 建议return RELOAD. 
     */  
    public abstract String save() throws Exception;
    
    public abstract String update() throws Exception;
  
    /** 
     * Action函数,删除Entity. 建议return RELOAD. 
     */  
    public abstract String delete() throws Exception;
  
    // -- Preparable函数 --//  
    /** 
     * 实现空的prepare()函数,屏蔽所有Action函数公共的二次绑定. 
     */  
    public void prepare() throws Exception {
    	System.out.println("二次绑定");
    }  
  
    /** 
     * 在input()前执行二次绑定. 
     */  
//    public void prepareUpdate() throws Exception {  
//       // prepareModel();  
//    	System.out.println("Crud prepareUpdate");
//    }  
  
    /** 
     * 在save()前执行二次绑定. 
     */  
//    public void prepareSave() throws Exception {  
//        prepareModel();  
//    }  
  
    /** 
     * 等同于prepare()的内部函数,供prepardMethodName()函数调用. 
     */  
//    protected abstract void prepareModel() throws Exception;  
    
    public Long getId() {
    	if(id==null){
    		id = Long.valueOf(ServletActionContext.getRequest().getParameter("id"));
    	}
		return id;
	}
    
    public void setId(Long id) {
		this.id = id;
	}
}
